##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

from PyQt4 import QtCore, QtGui


class Preferences(QtGui.QDialog):

    onApply = QtCore.pyqtSignal(dict)

    def __init__(self, widgets=[], conf=None, check=False, apply=True, parent=None):
        super(Preferences, self).__init__(parent)

        self.conf = conf
        if(conf is not None):
            if('name' in self.conf):
                self.setWindowTitle(self.conf['name'].replace('\n', ' '))
            else:
                self.setWindowTitle('Edit preferences')
        else:
            self.setWindowTitle('Edit preferences')

        self.setWindowModality(QtCore.Qt.ApplicationModal)
        self.setMaximumSize(635, 520)
        self.setMinimumSize(635, 520)
        self.resize(635, 520)

        self.listWidget = QtGui.QListWidget(self)
        self.listWidget.setGeometry(QtCore.QRect(5, 10, 185, 471))

        self.stackedWidget = QtGui.QStackedWidget(self)
        self.stackedWidget.setGeometry(QtCore.QRect(200, 10, 431, 471))

        for widget in widgets:
            self.listWidget.addItem(widget.NAME)
            widget = widget(QtCore.QSize(430, 465), self)
            widget.setConfiguration(self.conf)
            widget.enableCheck(check)
            self.stackedWidget.addWidget(widget)

        self.listWidget.currentItemChanged.connect(self.changePage)

        self.rejectButton = QtGui.QPushButton(self)
        self.rejectButton.setGeometry(QtCore.QRect(250, 490, 100, 25))
        self.rejectButton.setText("Cancel")

        self.applyButton = QtGui.QPushButton(self)
        self.applyButton.setGeometry(QtCore.QRect(370, 490, 100, 25))
        self.applyButton.setText("Apply")
        self.applyButton.setEnabled(apply)

        self.acceptButton = QtGui.QPushButton(self)
        self.acceptButton.setGeometry(QtCore.QRect(490, 490, 100, 25))
        self.acceptButton.setText("Ok")

        self.rejectButton.clicked.connect(self.reject)
        self.applyButton.clicked.connect(self.apply)
        self.acceptButton.clicked.connect(self.accept)

    def changePage(self, current, previous):
        if not current:
            current = previous

        self.stackedWidget.setCurrentIndex(self.listWidget.row(current))

    def accept(self):
        self.apply()
        super().accept()

    def apply(self):
        newConf = {}

        for n in range(self.stackedWidget.count()):
            newConf.update(self.stackedWidget.widget(n).getConfiguration())

        self.onApply.emit(newConf)
